<?php if(!defined('BASEPATH')) exit('No direct script access allowed!');

if ( !function_exists('_convert_date') ) 
{
	function _convert_date($date, $from_separator, $to_separator) {
		$parts = explode($from_separator, $date);
		if ( count($parts) != 3 || !(isset($parts[0]) && isset($parts[1]) && isset($parts[2])) ) {
			return '';
		}
		return str_pad($parts[2], 2, '0', STR_PAD_LEFT) .$to_separator. str_pad($parts[1], 2, '0', STR_PAD_LEFT) .$to_separator. str_pad($parts[0], 2, '0', STR_PAD_LEFT);
	}
}

if ( !function_exists('sql_to_indo_date') ) 
{
	function sql_to_indo_date($date) {
		// yyyy-mm-dd  ==>  dd/mm/yyyy
		// 0    1  2        2  1  0
		return _convert_date($date, '-', '/');
	}
}

if ( !function_exists('sql_to_indo_string') ) 
{
	function sql_to_indo_string($date) {
		$date  = sql_to_indo_date($date);
		$array = explode('/', $date);
		return $array[0] . ' ' . list_months_abbr(intval($array[1])) . ' ' . $array[2];
	}
}

if ( !function_exists('indo_to_indo_string') ) 
{
	function indo_to_indo_string($date) {
		$array = explode('/', $date);
		return $array[0] . ' ' . list_months_abbr(intval($array[1])) . ' ' . $array[2];
	}
}

if ( !function_exists('indo_to_sql_date') ) 
{
	function indo_to_sql_date($date) {
		// dd/mm/yyyy  ==>  yyyy-mm-dd
		// 0  1  2          2    1  0
		return _convert_date($date, '/', '-');
	}
}

if ( !function_exists('update_link') ) 
{
	function update_link($url, $label) {
		return anchor($url, $label, array('class'=>'update'));
	}
}

if ( !function_exists('delete_link') ) 
{
	function delete_link($url, $label) {
		return form_open($url) . form_submit('delete', $label, 'class="delete"') . form_close();
	}
}

if ( !function_exists('delete_batch_link') ) 
{
	function delete_batch_link($url, $label) {
		return form_open($url) . form_submit('delete', $label, 'class="delete_batch"') . form_close();
	}
}

if ( !function_exists('show_link') ) 
{
	function show_link($url, $label) {
		return anchor($url, $label, array('class'=>'show'));
	}
}

if ( !function_exists('add_new_link') ) 
{
	function add_new_link($url, $label) {
		return anchor($url, $label, array('class'=>'add'));
	}
}

if ( !function_exists('back_link') ) 
{
	function back_link($url, $label) {
		return anchor($url, $label, array('class'=>'back'));
	}
}

if ( !function_exists('sort_link') ) 
{
	function sort_link($url, $label) {
		return anchor($url, $label, array('class'=>'sort'));
	}
}

if ( !function_exists('copy_link') ) 
{
	function copy_link($url, $label) {
		return anchor($url, $label, array('class'=>'copy'));
	}
}

if ( !function_exists('info_box') ) 
{
	function info_box($msg,$w=30) {
$output = <<<EOF
<div class="ui-widget">
	<div class="ui-state-highlight ui-corner-all" style="margin:18px 0; padding:9px; width:{$w}%;"> 
		<p><span class="ui-icon ui-icon-info" style="float: left; margin-right:9px;"></span>
		$msg</p>
	</div>
</div>
EOF;
		return $output;
	}
}

if ( !function_exists('error_box') ) 
{
	function error_box($msg, $w=30) {
$output = <<<EOF
<div class="ui-widget">
	<div class="ui-state-error ui-corner-all" style="margin:18px 0; padding:9px; width:{$w}%;"> 
		<p><span class="ui-icon ui-icon-alert" style="float: left; margin-right:9px;"></span> 
		$msg</p>
	</div>
</div>
EOF;
		return $output;
	}
}

if ( !function_exists('isset_and_not_empty') ) 
{
	function isset_and_not_empty(&$var) {
		if (isset($var)) {
			if (is_array($var)) {
				if (count($var) > 0) {
					return true;
				}
			}else {
				if (!empty($var)) {
					return true;
				}
			}
		}
		return false;
	}
}

if ( !function_exists('isset_and_matches') )  
{
	function isset_and_matches(&$var, $value, $sametype=false) {
		if (isset($var)) {
			if ($sametype) {
				if($var === $value) {
					return true;
				}
			}else {
				if ($var == $value) {
					return true;
				}
			}
		}
		return false;
	}
}

if ( !function_exists('list_months') ) 
{
	function list_months($selected='') {
		$months = array('Januari', 'Februari', 'Maret', 'April', 'Mei', 'Juni', 'Juli',
			'Agustus', 'September', 'Oktober', 'November', 'Desember');
		if (!empty($selected)) {
			return $months[$selected-1];
		}
		return $months;
	}
}

if ( !function_exists('list_months_abbr') ) 
{
	function list_months_abbr($selected='') {
		$months = array('Jan', 'Feb', 'Mar', 'Apr', 'Mei', 'Jun', 'Jul',
			'Agu', 'Sep', 'Okt', 'Nov', 'Des');
		if (!empty($selected)) {
			return $months[$selected-1];
		}
		return $months;
	}
}

if ( !function_exists('list_months_from_to') ) 
{	
	function list_months_from_to($start_month, $end_month) {
		$months = list_months();
		return array_slice($months, $start_month-1, $end_month-$start_month+1);
	}
}

if ( !function_exists('extract_sql_date') )
{
	function extract_sql_date($date, $part='') {
		$array = explode('-', $date);
		if (count($array) != 3) {
			return '';
		}
		$adate = array('day' => $array[2], 'month' => $array[1], 'year' => $array[0]);
		if (!empty($part)) {
			switch ($part) {
			case 'd': return intval($adate['day']);
			case 'm': return intval($adate['month']);
			case 'y': return intval($adate['year']);
			default: return $adate;
			}
		}
		return $adate;
	}
}

if ( !function_exists('dayname_of') )
{
	function dayname_of($date) {
		$dates = array('Minggu', 'Senin', 'Selasa', 'Rabu', 'Kamis', 'Jumat', 'Sabtu');
		return $dates[date('w', strtotime($date))];
	}
}

if ( !function_exists('is_weekend_day') )
{
	function is_weekend_day($date) {
		$dayname = dayname_of($date);
		if ($dayname == 'Sabtu' || $dayname == 'Minggu') {
			return true;
		}
		return false;
	}
}

if ( !function_exists('fulldayname') ) {
	function fulldayname($abbr='') {
		$retval = '';
		switch ($abbr) {
			case 'sun': $retval = 'Minggu'; break;
			case 'mon': $retval = 'Senin'; break;
			case 'tue': $retval = 'Selasa'; break;
			case 'wed': $retval = 'Rabu'; break;
			case 'thu': $retval = 'Kamis'; break;
			case 'fri': $retval = 'Jumat'; break;
			case 'sat': $retval = 'Sabtu'; break;
		}
		return $retval;
	}
}

if ( !function_exists('current_dayname') ) {
	function current_dayname() {
		return strtolower(date('D'));
	}
}

if ( !function_exists('current_day_indo') ) {
	function current_day_indo() {
		return fulldayname(strtolower(date('D'))) . ', ' . sql_to_indo_string(date('Y-m-d'));
	}
}



if ( !function_exists('diff_status_date') )
{
	function diff_status_date($sql_date_1, $sql_date_2='') {
		$d1 = strtotime($sql_date_1);
		$d2 = 0;
		if (empty($sql_date_2)) {
			$d2 = strtotime('now');
		}else {
			$d2 = strtotime($sql_date_2);
		}
		if ($d1 < $d2) {
			return -1;
		}else if ($d1 > $d2) {
			return 1;
		}else {
			return 0;
		}
	}
}

if (!function_exists('my_date_diff')) {
	function my_date_diff($date_start, $date_end) {
		$d1 = strtotime($date_start.' 00:00:00');
		$d2 = strtotime($date_end.' 00:00:00');
		return round(abs($d2-$d1)/60/60/24);
	}
}

if (!function_exists('pagination')) {
	function pagination($records, $base_url, $rows_per_page, $current_page=1, $suffix_url='') {
		if (empty($current_page) || $current_page == 0) {
			$current_page = 1;
		}
		$total_records = count($records);
		$total_pages = intval($total_records / $rows_per_page);
		if ($total_records % $rows_per_page != 0) {
			$total_pages += 1;
		}
		$str = '<div class="pagination">';
		$str .= '<p>Halaman <strong>'.$current_page.'</strong> dari <strong>'.$total_pages.'</strong></p>';
		$str .= '<ul>';
		$str .= '<li><a href="'.$base_url.'?page=1'.(empty($suffix_url)?'':'&'.$suffix_url).'" title="halaman 1">&laquo;</a></li>';
		if ($current_page > 10) {
			$str .= '<li><a href="'.$base_url.'?page='.($current_page-10).(empty($suffix_url)?'':'&'.$suffix_url).'" title="halaman '.($current_page-10).'">'.($current_page-10).'</a></li>';
		}
		$start = $current_page-1; 
		$end   = $current_page+1;
		if ($current_page == 1) {
			$start = 1;
			$end   = $current_page+2 > $total_pages ? $total_pages : $current_page+2;
		}else if ($current_page == $total_pages) {
			$start = $current_page-2 < 0 ? 1 : $current_page-2;
			$end   = $total_pages;
		}
		for ($i=$start ; $i<=$end ; $i++) {
			$str .= '<li>';
			if ($current_page == $i) {
				$str .= $i;
			}else {
				$str .= '<a href="'.$base_url.'?page='.$i.(empty($suffix_url)?'':'&'.$suffix_url).'" title="halaman '.$i.'">'.$i.'</a>';
			}
			$str .= '</li>';
		}
		if ($current_page+10 < $total_pages) {
			$str .= '<li><a href="'.$base_url.'?page='.($current_page+10).(empty($suffix_url)?'':'&'.$suffix_url).'" title="halaman '.($current_page+10).'">'.($current_page+10).'</a></li>';
		}
		$str .= '<li><a href="'.$base_url.'?page='.$total_pages.(empty($suffix_url)?'':'&'.$suffix_url).'" title="halaman '.$total_pages.'">&raquo;</a></li>';
		$str .= '</ul>';
		$str .= '</div>';

		echo $str;
	}
}

if (!function_exists('pagination_records')) {
	function pagination_records(&$records, $current_page, $rows_per_page) {
		return array_slice($records, ($current_page-1)*$rows_per_page, $rows_per_page);
	}
}

if (!function_exists('is_date_between')) {
	function is_date_between($start_date, $end_date, $date) {
		$sdate = strtotime($start_date);
		$edate = strtotime($end_date);
		$ddate = strtotime($date);

		if ($ddate >= $sdate && $ddate <= $edate) {
			return true;
		}
		return false;
	}
}
